#ifndef THINK_THINK_MODEL_RELATION_BELONGSTOMANY_H
#define THINK_THINK_MODEL_RELATION_BELONGSTOMANY_H

static char* think_model_relation_belongstomany =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\model\\relation;\n"
	"\n"
	"use think\\Collection;\n"
	"use think\\Db;\n"
	"use think\\db\\Query;\n"
	"use think\\Exception;\n"
	"use think\\Loader;\n"
	"use think\\Model;\n"
	"use think\\model\\Pivot;\n"
	"use think\\model\\Relation;\n"
	"use think\\Paginator;\n"
	"\n"
	"class BelongsToMany extends Relation\n"
	"{\n"
	"    // 中间表表名\n"
	"    protected $middle;\n"
	"    // 中间表模型名称\n"
	"    protected $pivotName;\n"
	"    // 中间表模型对象\n"
	"    protected $pivot;\n"
	"    // 中间表数据名称\n"
	"    protected $pivotDataName = \'pivot\';\n"
	"\n"
	"    /**\n"
	"     * 构造函数\n"
	"     * @access public\n"
	"     * @param Model  $parent     上级模型对象\n"
	"     * @param string $model      模型名\n"
	"     * @param string $table      中间表名\n"
	"     * @param string $foreignKey 关联模型外键\n"
	"     * @param string $localKey   当前模型关联键\n"
	"     */\n"
	"    public function __construct(Model $parent, $model, $table, $foreignKey, $localKey)\n"
	"    {\n"
	"        $this->parent     = $parent;\n"
	"        $this->model      = $model;\n"
	"        $this->foreignKey = $foreignKey;\n"
	"        $this->localKey   = $localKey;\n"
	"        if (false !== strpos($table, \'\\\\\')) {\n"
	"            $this->pivotName = $table;\n"
	"            $this->middle    = basename(str_replace(\'\\\\\', \'/\', $table));\n"
	"        } else {\n"
	"            $this->middle = $table;\n"
	"        }\n"
	"        $this->query = (new $model)->db();\n"
	"        $this->pivot = $this->newPivot();\n"
	"\n"
	"        if (\'think\\model\\Pivot\' == get_class($this->pivot)) {\n"
	"            $this->pivot->name($this->middle);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置中间表模型\n"
	"     * @param $pivot\n"
	"     * @return $this\n"
	"     */\n"
	"    public function pivot($pivot)\n"
	"    {\n"
	"        $this->pivotName = $pivot;\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置中间表数据名称\n"
	"     * @access public\n"
	"     * @param  string $name\n"
	"     * @return $this\n"
	"     */\n"
	"    public function pivotDataName($name)\n"
	"    {\n"
	"        $this->pivotDataName = $name;\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取中间表更新条件\n"
	"     * @param $data\n"
	"     * @return array\n"
	"     */\n"
	"    protected function getUpdateWhere($data)\n"
	"    {\n"
	"        return [\n"
	"            $this->localKey   => $data[$this->localKey],\n"
	"            $this->foreignKey => $data[$this->foreignKey],\n"
	"        ];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 实例化中间表模型\n"
	"     * @param  array    $data\n"
	"     * @param  bool     $isUpdate\n"
	"     * @return Pivot\n"
	"     * @throws Exception\n"
	"     */\n"
	"    protected function newPivot($data = [], $isUpdate = false)\n"
	"    {\n"
	"        $class = $this->pivotName ?: \'\\\\think\\\\model\\\\Pivot\';\n"
	"        $pivot = new $class($data, $this->parent, $this->middle);\n"
	"        if ($pivot instanceof Pivot) {\n"
	"            return $isUpdate ? $pivot->isUpdate(true, $this->getUpdateWhere($data)) : $pivot;\n"
	"        } else {\n"
	"            throw new Exception(\'pivot model must extends: \\think\\model\\Pivot\');\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 合成中间表模型\n"
	"     * @param array|Collection|Paginator $models\n"
	"     */\n"
	"    protected function hydratePivot($models)\n"
	"    {\n"
	"        foreach ($models as $model) {\n"
	"            $pivot = [];\n"
	"            foreach ($model->getData() as $key => $val) {\n"
	"                if (strpos($key, \'__\')) {\n"
	"                    list($name, $attr) = explode(\'__\', $key, 2);\n"
	"                    if (\'pivot\' == $name) {\n"
	"                        $pivot[$attr] = $val;\n"
	"                        unset($model->$key);\n"
	"                    }\n"
	"                }\n"
	"            }\n"
	"            $model->setRelation($this->pivotDataName, $this->newPivot($pivot, true));\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建关联查询Query对象\n"
	"     * @return Query\n"
	"     */\n"
	"    protected function buildQuery()\n"
	"    {\n"
	"        $foreignKey = $this->foreignKey;\n"
	"        $localKey   = $this->localKey;\n"
	"        $pk         = $this->parent->getPk();\n"
	"        // 关联查询\n"
	"        $condition[\'pivot.\' . $localKey] = $this->parent->$pk;\n"
	"        return $this->belongsToManyQuery($foreignKey, $localKey, $condition);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 延迟获取关联数据\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包查询条件\n"
	"     * @return false|\\PDOStatement|string|\\think\\Collection\n"
	"     */\n"
	"    public function getRelation($subRelation = \'\', $closure = null)\n"
	"    {\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $this->query]);\n"
	"        }\n"
	"        $result = $this->buildQuery()->relation($subRelation)->select();\n"
	"        $this->hydratePivot($result);\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 重载select方法\n"
	"     * @param null $data\n"
	"     * @return false|\\PDOStatement|string|Collection\n"
	"     */\n"
	"    public function select($data = null)\n"
	"    {\n"
	"        $result = $this->buildQuery()->select($data);\n"
	"        $this->hydratePivot($result);\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 重载paginate方法\n"
	"     * @param null  $listRows\n"
	"     * @param bool  $simple\n"
	"     * @param array $config\n"
	"     * @return Paginator\n"
	"     */\n"
	"    public function paginate($listRows = null, $simple = false, $config = [])\n"
	"    {\n"
	"        $result = $this->buildQuery()->paginate($listRows, $simple, $config);\n"
	"        $this->hydratePivot($result);\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 重载find方法\n"
	"     * @param null $data\n"
	"     * @return array|false|\\PDOStatement|string|Model\n"
	"     */\n"
	"    public function find($data = null)\n"
	"    {\n"
	"        $result = $this->buildQuery()->find($data);\n"
	"        if ($result) {\n"
	"            $this->hydratePivot([$result]);\n"
	"        }\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 查找多条记录 如果不存在则抛出异常\n"
	"     * @access public\n"
	"     * @param array|string|Query|\\Closure $data\n"
	"     * @return array|\\PDOStatement|string|Model\n"
	"     */\n"
	"    public function selectOrFail($data = null)\n"
	"    {\n"
	"        return $this->failException(true)->select($data);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 查找单条记录 如果不存在则抛出异常\n"
	"     * @access public\n"
	"     * @param array|string|Query|\\Closure $data\n"
	"     * @return array|\\PDOStatement|string|Model\n"
	"     */\n"
	"    public function findOrFail($data = null)\n"
	"    {\n"
	"        return $this->failException(true)->find($data);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param string  $operator 比较操作符\n"
	"     * @param integer $count    个数\n"
	"     * @param string  $id       关联表的统计字段\n"
	"     * @param string  $joinType JOIN类型\n"
	"     * @return Query\n"
	"     */\n"
	"    public function has($operator = \'>=\', $count = 1, $id = \'*\', $joinType = \'INNER\')\n"
	"    {\n"
	"        return $this->parent;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param  mixed  $where 查询条件（数组或者闭包）\n"
	"     * @param  mixed  $fields   字段\n"
	"     * @return Query\n"
	"     * @throws Exception\n"
	"     */\n"
	"    public function hasWhere($where = [], $fields = null)\n"
	"    {\n"
	"        throw new Exception(\'relation not support: hasWhere\');\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置中间表的查询条件\n"
	"     * @param      $field\n"
	"     * @param null $op\n"
	"     * @param null $condition\n"
	"     * @return $this\n"
	"     */\n"
	"    public function wherePivot($field, $op = null, $condition = null)\n"
	"    {\n"
	"        $field = \'pivot.\' . $field;\n"
	"        $this->query->where($field, $op, $condition);\n"
	"        return $this;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（数据集）\n"
	"     * @access public\n"
	"     * @param array    $resultSet   数据集\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $localKey   = $this->localKey;\n"
	"        $foreignKey = $this->foreignKey;\n"
	"\n"
	"        $pk    = $resultSet[0]->getPk();\n"
	"        $range = [];\n"
	"        foreach ($resultSet as $result) {\n"
	"            // 获取关联外键列表\n"
	"            if (isset($result->$pk)) {\n"
	"                $range[] = $result->$pk;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if (!empty($range)) {\n"
	"            // 查询关联数据\n"
	"            $data = $this->eagerlyManyToMany([\n"
	"                \'pivot.\' . $localKey => [\n"
	"                    \'in\',\n"
	"                    $range,\n"
	"                ],\n"
	"            ], $relation, $subRelation);\n"
	"            // 关联属性名\n"
	"            $attr = Loader::parseName($relation);\n"
	"            // 关联数据封装\n"
	"            foreach ($resultSet as $result) {\n"
	"                if (!isset($data[$result->$pk])) {\n"
	"                    $data[$result->$pk] = [];\n"
	"                }\n"
	"\n"
	"                $result->setRelation($attr, $this->resultSetBuild($data[$result->$pk]));\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询（单个数据）\n"
	"     * @access public\n"
	"     * @param Model    $result      数据对象\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResult(&$result, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $pk = $result->getPk();\n"
	"        if (isset($result->$pk)) {\n"
	"            $pk = $result->$pk;\n"
	"            // 查询管理数据\n"
	"            $data = $this->eagerlyManyToMany([\'pivot.\' . $this->localKey => $pk], $relation, $subRelation);\n"
	"\n"
	"            // 关联数据封装\n"
	"            if (!isset($data[$pk])) {\n"
	"                $data[$pk] = [];\n"
	"            }\n"
	"            $result->setRelation(Loader::parseName($relation), $this->resultSetBuild($data[$pk]));\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 关联统计\n"
	"     * @access public\n"
	"     * @param Model    $result  数据对象\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @return integer\n"
	"     */\n"
	"    public function relationCount($result, $closure)\n"
	"    {\n"
	"        $pk    = $result->getPk();\n"
	"        $count = 0;\n"
	"        if (isset($result->$pk)) {\n"
	"            $pk    = $result->$pk;\n"
	"            $count = $this->belongsToManyQuery($this->foreignKey, $this->localKey, [\'pivot.\' . $this->localKey => $pk])->count();\n"
	"        }\n"
	"        return $count;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取关联统计子查询\n"
	"     * @access public\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @param string   $name    统计数据别名\n"
	"     * @return string\n"
	"     */\n"
	"    public function getRelationCountQuery($closure, &$name = null)\n"
	"    {\n"
	"        if ($closure) {\n"
	"            $return = call_user_func_array($closure, [ & $this->query]);\n"
	"            if ($return && is_string($return)) {\n"
	"                $name = $return;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [\n"
	"            \'pivot.\' . $this->localKey => [\n"
	"                \'exp\',\n"
	"                Db::raw(\'=\' . $this->parent->getTable() . \'.\' . $this->parent->getPk()),\n"
	"            ],\n"
	"        ])->fetchSql()->count();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 多对多 关联模型预查询\n"
	"     * @access public\n"
	"     * @param array  $where       关联预查询条件\n"
	"     * @param string $relation    关联名\n"
	"     * @param string $subRelation 子关联\n"
	"     * @return array\n"
	"     */\n"
	"    protected function eagerlyManyToMany($where, $relation, $subRelation = \'\')\n"
	"    {\n"
	"        // 预载入关联查询 支持嵌套预载入\n"
	"        $list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where)->with($subRelation)->select();\n"
	"\n"
	"        // 组装模型数据\n"
	"        $data = [];\n"
	"        foreach ($list as $set) {\n"
	"            $pivot = [];\n"
	"            foreach ($set->getData() as $key => $val) {\n"
	"                if (strpos($key, \'__\')) {\n"
	"                    list($name, $attr) = explode(\'__\', $key, 2);\n"
	"                    if (\'pivot\' == $name) {\n"
	"                        $pivot[$attr] = $val;\n"
	"                        unset($set->$key);\n"
	"                    }\n"
	"                }\n"
	"            }\n"
	"            $set->setRelation($this->pivotDataName, $this->newPivot($pivot, true));\n"
	"            $data[$pivot[$this->localKey]][] = $set;\n"
	"        }\n"
	"        return $data;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * BELONGS TO MANY 关联查询\n"
	"     * @access public\n"
	"     * @param string $foreignKey 关联模型关联键\n"
	"     * @param string $localKey   当前模型关联键\n"
	"     * @param array  $condition  关联查询条件\n"
	"     * @return Query\n"
	"     */\n"
	"    protected function belongsToManyQuery($foreignKey, $localKey, $condition = [])\n"
	"    {\n"
	"        // 关联查询封装\n"
	"        $tableName = $this->query->getTable();\n"
	"        $table     = $this->pivot->getTable();\n"
	"        $fields    = $this->getQueryFields($tableName);\n"
	"\n"
	"        $query = $this->query->field($fields)\n"
	"            ->field(true, false, $table, \'pivot\', \'pivot__\');\n"
	"\n"
	"        if (empty($this->baseQuery)) {\n"
	"            $relationFk = $this->query->getPk();\n"
	"            $query->join([$table => \'pivot\'], \'pivot.\' . $foreignKey . \'=\' . $tableName . \'.\' . $relationFk)\n"
	"                ->where($condition);\n"
	"        }\n"
	"        return $query;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 保存（新增）当前关联数据对象\n"
	"     * @access public\n"
	"     * @param mixed $data  数据 可以使用数组 关联模型对象 和 关联对象的主键\n"
	"     * @param array $pivot 中间表额外数据\n"
	"     * @return integer\n"
	"     */\n"
	"    public function save($data, array $pivot = [])\n"
	"    {\n"
	"        // 保存关联表/中间表数据\n"
	"        return $this->attach($data, $pivot);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 批量保存当前关联数据对象\n"
	"     * @access public\n"
	"     * @param array $dataSet   数据集\n"
	"     * @param array $pivot     中间表额外数据\n"
	"     * @param bool  $samePivot 额外数据是否相同\n"
	"     * @return integer\n"
	"     */\n"
	"    public function saveAll(array $dataSet, array $pivot = [], $samePivot = false)\n"
	"    {\n"
	"        $result = false;\n"
	"        foreach ($dataSet as $key => $data) {\n"
	"            if (!$samePivot) {\n"
	"                $pivotData = isset($pivot[$key]) ? $pivot[$key] : [];\n"
	"            } else {\n"
	"                $pivotData = $pivot;\n"
	"            }\n"
	"            $result = $this->attach($data, $pivotData);\n"
	"        }\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 附加关联的一个中间表数据\n"
	"     * @access public\n"
	"     * @param mixed $data  数据 可以使用数组、关联模型对象 或者 关联对象的主键\n"
	"     * @param array $pivot 中间表额外数据\n"
	"     * @return array|Pivot\n"
	"     * @throws Exception\n"
	"     */\n"
	"    public function attach($data, $pivot = [])\n"
	"    {\n"
	"        if (is_array($data)) {\n"
	"            if (key($data) === 0) {\n"
	"                $id = $data;\n"
	"            } else {\n"
	"                // 保存关联表数据\n"
	"                $model = new $this->model;\n"
	"                $model->save($data);\n"
	"                $id = $model->getLastInsID();\n"
	"            }\n"
	"        } elseif (is_numeric($data) || is_string($data)) {\n"
	"            // 根据关联表主键直接写入中间表\n"
	"            $id = $data;\n"
	"        } elseif ($data instanceof Model) {\n"
	"            // 根据关联表主键直接写入中间表\n"
	"            $relationFk = $data->getPk();\n"
	"            $id         = $data->$relationFk;\n"
	"        }\n"
	"\n"
	"        if ($id) {\n"
	"            // 保存中间表数据\n"
	"            $pk                     = $this->parent->getPk();\n"
	"            $pivot[$this->localKey] = $this->parent->$pk;\n"
	"            $ids                    = (array) $id;\n"
	"            foreach ($ids as $id) {\n"
	"                $pivot[$this->foreignKey] = $id;\n"
	"                $this->pivot->insert($pivot, true);\n"
	"                $result[] = $this->newPivot($pivot, true);\n"
	"            }\n"
	"            if (count($result) == 1) {\n"
	"                // 返回中间表模型对象\n"
	"                $result = $result[0];\n"
	"            }\n"
	"            return $result;\n"
	"        } else {\n"
	"            throw new Exception(\'miss relation data\');\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 判断是否存在关联数据\n"
	"     * @access public\n"
	"     * @param  mixed $data  数据 可以使用关联模型对象 或者 关联对象的主键\n"
	"     * @return Pivot\n"
	"     * @throws Exception\n"
	"     */\n"
	"    public function attached($data)\n"
	"    {\n"
	"        if ($data instanceof Model) {\n"
	"            $relationFk = $data->getPk();\n"
	"            $id         = $data->$relationFk;\n"
	"        } else {\n"
	"            $id = $data;\n"
	"        }\n"
	"\n"
	"        $pk = $this->parent->getPk();\n"
	"\n"
	"        $pivot = $this->pivot->where($this->localKey, $this->parent->$pk)->where($this->foreignKey, $id)->find();\n"
	"\n"
	"        return $pivot ?: false;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 解除关联的一个中间表数据\n"
	"     * @access public\n"
	"     * @param integer|array $data        数据 可以使用关联对象的主键\n"
	"     * @param bool          $relationDel 是否同时删除关联表数据\n"
	"     * @return integer\n"
	"     */\n"
	"    public function detach($data = null, $relationDel = false)\n"
	"    {\n"
	"        if (is_array($data)) {\n"
	"            $id = $data;\n"
	"        } elseif (is_numeric($data) || is_string($data)) {\n"
	"            // 根据关联表主键直接写入中间表\n"
	"            $id = $data;\n"
	"        } elseif ($data instanceof Model) {\n"
	"            // 根据关联表主键直接写入中间表\n"
	"            $relationFk = $data->getPk();\n"
	"            $id         = $data->$relationFk;\n"
	"        }\n"
	"        // 删除中间表数据\n"
	"        $pk                     = $this->parent->getPk();\n"
	"        $pivot[$this->localKey] = $this->parent->$pk;\n"
	"        if (isset($id)) {\n"
	"            $pivot[$this->foreignKey] = is_array($id) ? [\'in\', $id] : $id;\n"
	"        }\n"
	"        $this->pivot->where($pivot)->delete();\n"
	"        // 删除关联表数据\n"
	"        if (isset($id) && $relationDel) {\n"
	"            $model = $this->model;\n"
	"            $model::destroy($id);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 数据同步\n"
	"     * @param array $ids\n"
	"     * @param bool  $detaching\n"
	"     * @return array\n"
	"     */\n"
	"    public function sync($ids, $detaching = true)\n"
	"    {\n"
	"        $changes = [\n"
	"            \'attached\' => [],\n"
	"            \'detached\' => [],\n"
	"            \'updated\'  => [],\n"
	"        ];\n"
	"        $pk      = $this->parent->getPk();\n"
	"        $current = $this->pivot->where($this->localKey, $this->parent->$pk)\n"
	"            ->column($this->foreignKey);\n"
	"        $records = [];\n"
	"\n"
	"        foreach ($ids as $key => $value) {\n"
	"            if (!is_array($value)) {\n"
	"                $records[$value] = [];\n"
	"            } else {\n"
	"                $records[$key] = $value;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        $detach = array_diff($current, array_keys($records));\n"
	"\n"
	"        if ($detaching && count($detach) > 0) {\n"
	"            $this->detach($detach);\n"
	"\n"
	"            $changes[\'detached\'] = $detach;\n"
	"        }\n"
	"\n"
	"        foreach ($records as $id => $attributes) {\n"
	"            if (!in_array($id, $current)) {\n"
	"                $this->attach($id, $attributes);\n"
	"                $changes[\'attached\'][] = $id;\n"
	"            } elseif (count($attributes) > 0 &&\n"
	"                $this->attach($id, $attributes)\n"
	"            ) {\n"
	"                $changes[\'updated\'][] = $id;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        return $changes;\n"
	"\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 执行基础查询（进执行一次）\n"
	"     * @access protected\n"
	"     * @return void\n"
	"     */\n"
	"    protected function baseQuery()\n"
	"    {\n"
	"        if (empty($this->baseQuery) && $this->parent->getData()) {\n"
	"            $pk    = $this->parent->getPk();\n"
	"            $table = $this->pivot->getTable();\n"
	"            $this->query->join([$table => \'pivot\'], \'pivot.\' . $this->foreignKey . \'=\' . $this->query->getTable() . \'.\' . $this->query->getPk())->where(\'pivot.\' . $this->localKey, $this->parent->$pk);\n"
	"            $this->baseQuery = true;\n"
	"        }\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif